package ai.tubes.iris;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class FeedbackActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		
		alertDialog = new AlertDialog.Builder(this).create();
		
		mesininferensi = new ai.tubes.iris.knowledge.inferenceengine();
		
		TableField = mesininferensi.getInferenceTable();
		
		organ = (EditText) findViewById(R.id.txtorgan);
		nilai = (EditText) findViewById(R.id.txtbefore);
		result = (EditText) findViewById(R.id.txtresult);
		
		
		
		/*button submit*/
		mSubmit	= (Button)findViewById(R.id.buttonsubmit);
		mSubmit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				getText();
				int numoforg = searchOrgan(organ.getText().toString());
				if (numoforg != -1)
				{
					float valuebwh = new Float(TableField[1][numoforg]);
					float valueats = new Float(TableField[2][numoforg]);
					if (retval.equalsIgnoreCase("Lemah"))
					{
						if (value < valueats)
							{mesininferensi.changeInferenceStatus(org, value, valueats);}
						else if(value == valueats || value > valueats)
							{mesininferensi.changeInferenceStatus(org, value, (value+2));}
						mesininferensi.addLearningData(org, "Lemah", value);
						mesininferensi.writeInferenceFile();
						showmessagebox("Submiting feedback has successfull.");
					}
					else if (retval.equalsIgnoreCase("Sedang"))
					{	
						if (value < valuebwh)
							{mesininferensi.changeInferenceStatus(org, value, valueats);}
						else if (value > valueats)
							{mesininferensi.changeInferenceStatus(org, value, value+2);}
						mesininferensi.addLearningData(org, "Sedang", value);
						mesininferensi.writeInferenceFile();
						showmessagebox("Submiting feedback has successfull.");
					}
					else if(retval.equalsIgnoreCase("Baik"))
					{
						if (value > valuebwh)
							{mesininferensi.changeInferenceStatus(org, valuebwh, value);}
						else 
							{mesininferensi.changeInferenceStatus(org, value-2, value);}
						mesininferensi.addLearningData(org, "Baik", value);
						mesininferensi.writeInferenceFile();
						showmessagebox("Submiting feedback has successfull.");
					}
					else
					{
						showmessagebox("Input false.");
					}
				}
			}
		});
		
		/*button back*/
		mBack	= (Button)findViewById(R.id.buttonback);
		mBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				startActivity(new Intent(FeedbackActivity.this.getApplicationContext(), ResultActivity.class));
			}
		});
		
	}
	
	private void showmessagebox(String Message)
	{
		alertDialog.setMessage(Message);
		alertDialog.show();	
	}
	
	private int searchOrgan(String organ)
	{
		for (int i = 0; i < 19; i++) {
            if (TableField[0][i].equalsIgnoreCase(organ)) //jika nama organ ketemu
            {
                return i;
            }
        }
		return -1;
	}
	
	private void getText()
	{
		org = organ.getText().toString();
		retval = result.getText().toString();
		value = new Float(nilai.getText().toString());
	}
	
	AlertDialog alertDialog;
	String[][] TableField;
	ai.tubes.iris.knowledge.inferenceengine mesininferensi;
	private Button mSubmit, mBack;
	private EditText organ, nilai, result;
	private String org, retval;
	private float value;
}

